/*******************************************************************************
  EmiSnes - Super Nintendo Entertainment System (TM) emulator for Symbian.

  EmiSnes is port of Snes9x see Snes9x headers for specific licence.

  Many thanks to the whole Snes9x team for the great work.

  EmiSnes is licenced under GPLv2.

  Copyright 2011 - 2016 Andre Botelho (andrebotelhomail@gmail.com)

  EmiSnes homepage: http://libproductions.wordpress.com/emisnes

  Permission to use, copy, modify and distribute EmiSnes in both binary and
  source form, for non-commercial purposes, is hereby granted without fee,
  providing that this license information and copyright notice appear with
  all copies and any derived work.

  This software source is provided 'as-is', without any express or implied
  warranty. In no event shall the authors be held liable for any damages
  arising from the use of this software.

  EmiSnes is freeware for PERSONAL USE only. Commercial users should
  seek permission of the copyright holder first. Commercial use includes
  charging money for EmiSnes or software derived from EmiSnes.

  The copyright holder request that bug fixes and improvements to the code
  should be forwarded to them so everyone can benefit from the modifications
  in future versions.

  Super NES and Super Nintendo Entertainment System are trademarks of
  Nintendo Co., Limited and its subsidiary companies.
*******************************************************************************/

#include "emuinterface.h"
#include "mainwindow.h"
#include <QAudioFormat>
#include <QAudioDeviceInfo>

QByteArray *emuAudioBuffer;
QImage *emuFramebuffer;

extern void S9xLoadState();
extern void S9xSaveState();
int snesInit();
int snesLoadRom(char*);
int snesRun();
EmuType currentEmu;
void snesGetChanges();
void sneUpdate_audio(int len);
void snesKeyprocess( int, bool);
extern QImage *snesFrameBuffer;
extern QByteArray *snesAudioBuffer;
extern "C" void S9xReset();
extern "C" void S9xExit();
extern MainWindow *emuWindow;


int emuInit( EmuType type){
    currentEmu = type;
    snesInit();
    if( snesFrameBuffer) emuFramebuffer = snesFrameBuffer;
    if( snesAudioBuffer) emuAudioBuffer = snesAudioBuffer;
    //InitAudio();
}

int InitAudio(){
    QAudioFormat format;
    format.setFrequency(22050);
    format.setChannels(2);
    format.setSampleSize(16);
    format.setCodec("audio/pcm");
    format.setByteOrder(QAudioFormat::LittleEndian);
    format.setSampleType(QAudioFormat::SignedInt);

    QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());

    emuWindow->audioOut = new QAudioOutput(info, format, emuWindow);
    if(emuWindow->audioOut){
        emuWindow->setDevice();
        return 1;
    }else{
        return 0;
    }
}


int emuLoadRom( QString * romFile ){
    if( romFile->isEmpty()) return 0;
    return !snesLoadRom(romFile->toLatin1().data());
}

int emuClose(){
    S9xExit();
}


int emuLoop(){
    snesRun();
}

int emuReset(){
    S9xReset();
}


void emuSaveState(){
    S9xSaveState();
}


void emuLoadState(){
    S9xLoadState();
}

int emuProcessKeys( int key, bool press){
    snesKeyprocess(key , press);
}

